package com.fortune.service.impl;

import com.fortune.dao.COrderBaseDao;
import com.fortune.dto.in.cOrderMgt.COrderAddDto;
import com.fortune.dto.in.cOrderMgt.COrderApproveDto;
import com.fortune.dto.in.cOrderMgt.COrderEditDto;
import com.fortune.dto.in.cOrderMgt.COrderListDto;
import com.fortune.dto.in.clientRepayment.ClientRepaymentConfirm;
import com.fortune.dto.in.clientRepayment.ClientRepaymentDetails;
import com.fortune.dto.in.clientRepayment.ClientRepaymentList;
import com.fortune.dto.in.clientRepayment.piComfirm;
import com.fortune.service.COrderBaseService;
import com.fortune.vo.cOrderMgt.*;
import com.fortune.vo.clientRepayment.ClientRepaymentDetailsVo;
import com.fortune.vo.clientRepayment.ClientRepaymentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class COrderBaseServiceImpl implements COrderBaseService {
    @Autowired
    private COrderBaseDao cOrderBaseDao;

    /**
     * 查询客户订单列表
     * @param dealerId
     * @param listDto
     * @return
     */
    @Override
    public List<COrder> queryOrderBaseList(String dealerId, COrderListDto listDto) {
        return cOrderBaseDao.queryOrderBaseList(dealerId,listDto);
    }

    /**
     * 查询客户订单列表个数
     * @param dealerId
     * @param listDto
     * @return
     */
    @Override
    public int queryOrderBaseListCount(String dealerId, COrderListDto listDto) {
        return cOrderBaseDao.queryOrderBaseListCount(dealerId,listDto);
    }

    /**
     * 添加客户订单
     * @param dealerId
     * @param addDto
     * @return
     */
    @Override
    @Transactional
    public int addOrderBase(String dealerId, COrderAddDto addDto) {
        //订单id
        String id = UUID.randomUUID().toString().replace("-","");
        int num = cOrderBaseDao.addOrderBase(id,dealerId,addDto);
        if (num < 1){
            throw new RuntimeException("添加客户订单失败");
        }
        num = cOrderBaseDao.addOrderBaseDetails(addDto.getId(),id,dealerId,addDto.getProduce());
        if (num < 1){
            throw new RuntimeException("添加客户订单失败");
        }
        return 1;
    }

    /**
     * 编辑客户订单
     * @param dealerId
     * @param editDto
     * @return
     */
    @Override
    @Transactional
    public int editOrderBase(String dealerId, COrderEditDto editDto) {
        int num = cOrderBaseDao.editOrderBase(dealerId,editDto);
        if (num < 1){
            throw new RuntimeException("编辑客户订单失败");
        }
        num = cOrderBaseDao.deleteOrderBase(dealerId,editDto.getId(),editDto.getOrdersId());
        if (num < 1){
            throw new RuntimeException("编辑客户订单失败");
        }
        num = cOrderBaseDao.addOrderBaseDetails(editDto.getId(),editDto.getOrdersId(),dealerId,editDto.getProduce());
        if (num < 1){
            throw new RuntimeException("编辑客户订单失败");
        }
        return 1;
    }

    /**
     * 删除客户订单
     * @param dealerId
     * @param orderId
     * @param clientId
     * @return
     */
    @Override
    public int deleteOrderBase(String dealerId, String orderId, String clientId) {
        return cOrderBaseDao.deleteOrder(dealerId,orderId,clientId);
    }

    /**
     * 批准发货
     * @param dealerId
     * @param userId
     * @param cOrderApproveDto
     * @return
     */
    @Override
    @Transactional
    public int updateApproved(String dealerId,String userId,COrderApproveDto cOrderApproveDto) {
        COrderBaseInfo orderBaseInfo = cOrderBaseDao.queryOrderBaseInfo(dealerId,cOrderApproveDto.getClientId(),cOrderApproveDto.getOrderId());
//        int maxNum = cOrderBaseDao.queryMaxNum();
        //出库单ID
        String id = UUID.randomUUID().toString().replace("-","");

        Integer inNum = cOrderBaseDao.queryDOutBaseInfoNo(dealerId);
        String endNum = "CK000001";
        if (inNum!= null){
            StringBuffer string = new StringBuffer();
            if (inNum.toString().length()!=6){
                for (int i = 0; i < 6-inNum.toString().length();i++){
                    string.append("0");
                }
            }
            endNum = "CK" + string.toString() + (inNum+1);
        }
        int num = cOrderBaseDao.addOrderBaseInfo(userId,id,cOrderApproveDto,orderBaseInfo,endNum);
        if (num < 1){
            throw new RuntimeException("批准发货失败");
        }
        num = cOrderBaseDao.addOrderBaseDetail(id,orderBaseInfo.getPriduces());
        if (num < 1){
            throw new RuntimeException("批准发货失败");
        }
        num = cOrderBaseDao.updateApproved(dealerId,userId,cOrderApproveDto);
        if (num < 1){
            throw new RuntimeException("批准发货失败");
        }
        return num;
    }

    /**
     * 详情
     * @param dealerId
     * @param orderId
     * @param clientId
     * @return
     */
    @Override
    public COrderDetails queryDetails(String dealerId, String orderId, String clientId) {
        return cOrderBaseDao.queryDetails(dealerId,orderId,clientId);
    }

    /**
     * 查询经销商司机
     * @param dealerId
     * @param driverId
     * @return
     */
    @Override
    public List<COrderDriver> queryDriverList(String dealerId, String driverId) {
        return cOrderBaseDao.queryDriverList(dealerId,driverId);
    }

    /**
     * 开单人信息
     * @param dealerId
     * @param userId
     * @return
     */
    @Override
    public COrderBiller queryBiller(String dealerId, String userId) {
        return cOrderBaseDao.queryBiller(dealerId,userId);
    }

    /**
     *
     * @param dealerId
     * @param listDto
     * @return
     */
    @Override
    public Map queryClientRepaymentList(String dealerId, ClientRepaymentList listDto) {
        List<ClientRepaymentVo> list =  cOrderBaseDao.queryClientRepaymentList(dealerId,listDto);
        BigDecimal aa ;
        for(ClientRepaymentVo vo :list){
            if(vo.getPay().toString().equals("YES")){
                aa = vo.getpTotalPrice().add(vo.getdTotalPrice());
                vo.setAa(aa);
            }else{
                vo.setAa(new BigDecimal(0));
            }
        }
        HashMap map = new HashMap();
        map.put("list",list);
        return map;
    }

    /**
     *
     * @param dealerId
     * @param listDto
     * @return
     */
    @Override
    public int queryClientRepaymentListCount(String dealerId, ClientRepaymentList listDto) {
        return cOrderBaseDao.queryClientRepaymentListCount(dealerId,listDto);
    }

    /**
     * 确认回款
     * @param dealerId
     * @param clientRepaymentConfirm
     * @return
     */
    @Override
    public int clientRepaymentConfirm(String userId, String dealerId, ClientRepaymentConfirm clientRepaymentConfirm) {
        return cOrderBaseDao.clientRepaymentConfirm(userId,dealerId,clientRepaymentConfirm);
    }

    /**
     * 批量确认回款
     * @param dealerId
     * @param piComfirm
     * @return
     */
    @Override
    public int clientRepaymentPiConfirm(String userId,String dealerId, piComfirm piComfirm) {
        return cOrderBaseDao.clientRepaymentPiConfirm(userId,dealerId,piComfirm.getList());
    }

    @Override
    public ClientRepaymentDetailsVo queryClientRepaymentDetails(String dealerId, ClientRepaymentDetails details) {
        return cOrderBaseDao.queryClientRepaymentDetails(dealerId,details);
    }


}
